package com.device.operating.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.device.operating.entity.Domain;
import com.device.operating.entity.User;
import com.device.operating.service.EmService;
import com.device.operating.utils.KeyProvider;

@Controller
public class CacheManagerController {
    static CacheManager manager;
    static Cache cache;
    
    @Resource(name="EmService")
    private EmService EmService;
    
    public Map startCache(HttpServletRequest request){
    	Map map=new HashMap<>();
    	String cachename=request.getParameter("cachename");
    	String url=request.getSession().getServletContext().getRealPath("ehcacheFile");
    	manager=new CacheManager(url+"/"+cachename+"./xml");
    	cache=manager.getCache(cachename);
    	
    	return map;
    }
    @RequestMapping("/putCacheElements")
    public void putCacheElements(HttpServletRequest request){
    	String url=request.getSession().getServletContext().getRealPath("ehcacheFile");
    	String cachename=request.getParameter("cachename");
    	String liststr=request.getParameter("list");
    	Cache cache = manager.getCache(cachename);
    	JSONObject json=JSONObject.fromObject(liststr);
    	for(Object obj:json.keySet()){
    		String key=obj.toString();
    		Element ele=new Element(key,json.get(key));
    		cache.put(ele);
    	}
    }
    
    @RequestMapping("/getdomains")
    @ResponseBody
    public List<Object> getdomains(HttpServletRequest request){
    	String userId=KeyProvider.getPrimaryKey();
    	User user=new User();
    	user.setUserId(userId);
    	user.setUsername("sdfsdf");
    	EmService.saveEntity(user);
    	
    	
    	String fym=request.getParameter("firmId");
    	List list=EmService.findEntitylist("domain", "fym",fym );
    	for(Object obj:list){
    		Domain dm=(Domain)obj;
    		dm.getFirmId();
    	}
    	return list;
    }
}
